package com.data.king.tenda.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.data.king.tenda.MyApplication;
import com.data.king.tenda.bean.TendaData;
import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lenovo on 2018/1/9.
 */
public class TendaService extends Service {

    public static final String TAG = "TendaService";
    private static ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
    @Override
    public void onCreate() {
        super.onCreate();
        pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Util.getAsynHttp();
            }
        },1L,180L, TimeUnit.SECONDS);
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //MyApplication myApplication = (MyApplication) this.getApplication();
        Log.d(TAG, "onStartCommand() executed");
        //Log.d(TAG,"获取到的请求地址：" + myApplication.getTENDA_URL());
        if(pool.isTerminated()){
            pool.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    Util.getAsynHttp();
                }
            },1L,180L, TimeUnit.SECONDS);
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        pool.shutdown();
        super.onDestroy();
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



}
